/**
 * 版权所有(C)，上海勾芒信息科技，2016，所有权利保留。
 * 
 * 项目名：	carwx
 * 文件名：	TemplateMsgReplyTool.java
 * 模块说明：	
 * 修改历史：
 * 2016年12月1日 - Debenson - 创建。
 */
package com.alex.wechat.mp.handler;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.core.util.ContextHolderUtils;
import org.jeecgframework.web.cgform.common.CgAutoListConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alex.wechat.mp.message.news.entity.WxNewsItem;
import com.alex.wechat.mp.message.news.service.WxNewsItemServiceI;
import com.alex.wechat.mp.message.text.entity.WxTextTemplate;
import com.alex.wechat.mp.message.text.service.WxTextTemplateServiceI;
import com.alex.wechat.mp.util.TextTemplateTool;
import com.gomore.jeecg.common.Assert;

import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import me.chanjar.weixin.mp.builder.outxml.NewsBuilder;

/**
 * 模板消息回复工具。
 * 
 * @author Debenson
 * @since 0.1
 */
@Component("templateMsgReplyTool")
public class TemplateMsgReplyTool {

  @Autowired
  private WxTextTemplateServiceI textTemplateService;
  @Autowired
  private TextTemplateTool textTemplateTool;
  @Autowired
  private WxNewsItemServiceI newsItemService;

  public WxMpXmlOutMessage replyTextMsg(WxMpXmlMessage wxMessage, String templateId) {
    Assert.assertArgumentNotEmpty(templateId, "templateId");

    WxTextTemplate textTemplate = textTemplateService.get(WxTextTemplate.class, templateId);
    if (textTemplate == null) {
      throw new RuntimeException("找不到指定的文本模板");
    }

    String content = textTemplate.getContent();
    if (CgAutoListConstant.BOOL_TRUE.equals(textTemplate.getFreemarkerSupport())) {
      content = textTemplateTool.resolve(content);
    }
    return WxMpXmlOutMessage.TEXT().content(content).fromUser(wxMessage.getToUser())
        .toUser(wxMessage.getFromUser()).build();
  }

  public WxMpXmlOutMessage replyNewsMsg(WxMpXmlMessage wxMessage, String templateId) {
    Assert.assertArgumentNotEmpty(templateId, "templateId");

    NewsBuilder builder = WxMpXmlOutMessage.NEWS().fromUser(wxMessage.getToUser())
        .toUser(wxMessage.getFromUser());
    List<WxNewsItem> items = newsItemService.getNewsItem(templateId);
    for (WxNewsItem newsitem : items) {
      WxMpXmlOutNewsMessage.Item destItem = new WxMpXmlOutNewsMessage.Item();
      destItem.setTitle(newsitem.getTitle());
      destItem.setDescription(newsitem.getDescription());
      destItem.setPicUrl(newsitem.getThumbUrl());
      if (StringUtils.isNotBlank(newsitem.getUrl())) {
        // 如果置了外部链接，则点击查看直接跳转外部链接
        String realUrl = textTemplateTool.resolve(newsitem.getUrl());
        destItem.setUrl(realUrl);
      } else {
        // 否则，就查看图文内容
        destItem.setUrl(newsitem.viewUrl(ContextHolderUtils.getRequest()));
      }
      builder.addArticle(destItem);
    }
    return builder.build();
  }

}
